﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.Serialization.Json;
using System.Xml;
using System.Collections.Generic;

/// <summary>
///SearchAPI 的摘要说明
/// </summary>
public class SearchAPI
{
    private static string Channel = "LHJ";
    private static string Key = "EF1543A5EEEF106871A80A91927FE04E1E4FBE70";
    private static string Url = "http://www.mangocity.com/HDL/service/MZExHotel";

    private static string getXMLHeader()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        sb.AppendLine("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:dto=\"http://www.mangocity.com/hdl/ex/dto\">");
        sb.AppendLine("<soapenv:Header/>");
        sb.AppendLine("<soapenv:Body>");
        return sb.ToString();
    }

    private static string getXMLFooter()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("</soapenv:Body>");
        sb.AppendLine("</soapenv:Envelope>");
        return sb.ToString();
    }

    public static string GetMultiSearchXML(string cityCode, string checkInDate, string checkOutDate)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine(getXMLHeader());
        sb.AppendLine("<dto:MutilHotelRequest>");
        sb.AppendLine("<dto:author>");
        sb.AppendLine("<dto:channel>" + Channel + "</dto:channel>");
        sb.AppendLine("<dto:key>" + Key + "</dto:key>");
        sb.AppendLine("<dto:timeStamp>" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "</dto:timeStamp>");
        sb.AppendLine("</dto:author>");
        sb.AppendLine("<dto:cityCode>" + cityCode + "</dto:cityCode>");
        sb.AppendLine("<dto:checkInDate>" + checkInDate + "</dto:checkInDate>");
        sb.AppendLine("<dto:checkOutDate>" + checkOutDate + "</dto:checkOutDate>");
        sb.AppendLine("</dto:MutilHotelRequest>");
        sb.AppendLine(getXMLFooter());
        return sb.ToString();
    }

    public static string GetSingleSearchXML(string cityCode, string hotelCode, string checkInDate, string checkOutDate)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine(getXMLHeader());
        sb.AppendLine("<dto:SingleHotelRequest>");
        sb.AppendLine("<dto:author>");
        sb.AppendLine("<dto:channel>" + Channel + "</dto:channel>");
        sb.AppendLine("<dto:key>" + Key + "</dto:key>");
        sb.AppendLine("<dto:timeStamp>" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "</dto:timeStamp>");
        sb.AppendLine("</dto:author>");
        sb.AppendLine("<dto:cityCode>" + cityCode + "</dto:cityCode>");
        sb.AppendLine("<dto:hotelCode>" + hotelCode + "</dto:hotelCode>");
        sb.AppendLine("<dto:checkInDate>" + checkInDate + "</dto:checkInDate>");
        sb.AppendLine("<dto:checkOutDate>" + checkOutDate + "</dto:checkOutDate>");
        sb.AppendLine("</dto:SingleHotelRequest>");
        sb.AppendLine(getXMLFooter());
        return sb.ToString();
    }

    public static DataSet GetResonseDataSet(string postXml)
    {
        try
        {
            //Our postvars
            byte[] buffer = System.Text.Encoding.ASCII.GetBytes(postXml);
            //Initialisation, we use localhost, change if appliable
            System.Net.HttpWebRequest WebReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);
            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "text/xml";
            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;
            //We open a stream for writing the postvars
            System.IO.Stream postData = WebReq.GetRequestStream();
            //Now we write, and afterwards, we close. Closing is always important!
            postData.Write(buffer, 0, buffer.Length);
            postData.Close();
            //Get the response handle, we have no true response yet!
            System.Net.HttpWebResponse WebResp = (System.Net.HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            //Console.WriteLine(WebResp.StatusCode);
            //Console.WriteLine(WebResp.Server);

            //Now, we read the response (the string), and output it.
            using (System.IO.Stream answer = WebResp.GetResponseStream())
            {
                //System.IO.StreamReader reader = new System.IO.StreamReader(answer);
                //string res = reader.ReadToEnd();  
                DataSet ds = new DataSet();              
                ds.ReadXml(answer);
                return ds;
            }
            //System.Data.DataSet ds = new System.Data.DataSet();
            //ds.ReadXml(answer);
            //if (ds != null && ds.Tables["hotelList"] != null)
            //{
            //    string json = new SerializeHelper().ToJson<System.Data.DataTable>(ds.Tables["hotelList"]);
            //}
            //return true;
        }
        catch (Exception ex)
        { }
        return null;
    }

    public static string GetResonseXml(string postXml)
    {
        try
        {
            //Our postvars
            byte[] buffer = System.Text.Encoding.ASCII.GetBytes(postXml);
            //Initialisation, we use localhost, change if appliable
            System.Net.HttpWebRequest WebReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);
            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "text/xml";
            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;
            //We open a stream for writing the postvars
            System.IO.Stream postData = WebReq.GetRequestStream();
            //Now we write, and afterwards, we close. Closing is always important!
            postData.Write(buffer, 0, buffer.Length);
            postData.Close();
            //Get the response handle, we have no true response yet!
            System.Net.HttpWebResponse WebResp = (System.Net.HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            //Console.WriteLine(WebResp.StatusCode);
            //Console.WriteLine(WebResp.Server);

            //Now, we read the response (the string), and output it.
            using (System.IO.Stream answer = WebResp.GetResponseStream())
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(answer);
                return reader.ReadToEnd();  
            }
        }
        catch (Exception ex)
        { }
        return string.Empty;
    }

    public static DataSet SearchHotelList(string cityCode, string checkInDate, string checkOutDate, string strWhere, string strOrder, int pageIndex, int pageSize, out int totalRec)
    {
        totalRec = 0;
        try
        {
            string postXml = SearchAPI.GetMultiSearchXML(cityCode, checkInDate, checkOutDate);
            string resXml = SearchAPI.GetResonseXml(postXml);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(resXml);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            nsmgr.AddNamespace("MutilHotelResponse", "http://www.mangocity.com/hdl/ex/dto");
            List<BLV.Model.HotelInfo> htList = XmlHelper<BLV.Model.HotelInfo>.XmlToObjList<BLV.Model.HotelInfo>(doc, "hotelList", nsmgr);

            BLV.BLL.HotelInfo bll = new BLV.BLL.HotelInfo();

            if (htList != null)
            {
                foreach (BLV.Model.HotelInfo ht in htList)
                {
                    if (!bll.Exists(ht.hotelCode))
                    { bll.Add(ht); }
                }

                if (!string.IsNullOrEmpty(strWhere))
                {
                    strWhere += " and ";
                    strWhere += "city='" + cityCode + "'";
                }                
                return bll.GetPagingList(strWhere, strOrder, pageIndex, pageSize, out totalRec);
            }
        }
        catch { }
        return null;
    }
}
